import allure
from page_base.page_base import Base
from page_element.el_roo import *
from utils.timer import sleep
import logging



class PageLogin(Base):
    """登录页面"""
    def page_login(self, username, password, casename, expected):
        # 打开A3mall
        self.open_url(url)
        self.maximize_window()

        with allure.step(f"输入用户名: {username}"):
            self.send_keys(roo_login_page.name_input, username)
        with allure.step(f"输入密码: {password}"):
            self.send_keys(roo_login_page.pwd_input, password)
        # 强制等待输入验证码
        sleep(10)
        with allure.step("点击登录按钮"):
            self.click(roo_login_page.login_btn)
        # 断言
        with allure.step("验证登录"):
            if '登录成功' in casename:
                dy = self.text(roo_login_page.dysuccess)
                logging.info(f"预期结果：{expected}，实际结果: {dy}")
                assert expected == dy, f"断言失败(验证失败): 期望包含'{expected}'，实际得到'{dy}'"
                logging.info(f"验证成功,{casename}")
            else:
                dy = self.text(roo_login_page.dyfail)
                logging.info(f"预期结果：{expected}，实际结果: {dy}")
                assert expected == dy, f"断言失败(验证失败): 期望等于'{expected}'，实际得到'{dy}'"
                logging.info(f"验证成功,{casename}")

    def login(self):
        self.open_url(url)
        self.maximize_window()

        with allure.step(f"输入用户名"):
            self.send_keys(roo_login_page.name_input,"admin")
        with allure.step(f"输入密码"):
            self.send_keys(roo_login_page.pwd_input, "123456")
        # 强制等待输入验证码
        sleep(10)
        with allure.step("点击登录按钮"):
            self.click(roo_login_page.login_btn)

        # 断言
        dy = self.text(roo_login_page.dysuccess)
        assert "系统菜单" == dy
        logging.info(f"验证成功,登录")



